package com.desire.test;

import java.util.*;

/**
 * @author desire
 */
public class _14_Map集合 {

    public static void main(String[] args) {
        //可以直接传入比较器(指定规则) 进行排序
        Map<Integer, String> m = new TreeMap<>((a, b) -> b - a);
        m.put(1, "java1");
        m.put(3, "java122");
        m.put(10, "java2");
        m.put(4, "java4");
        System.out.println(m.size());
        System.out.println(m);
    }

    public static void main5(String[] args) {
        /**
         * treeMap会自动进行排序
         */
        Map<String, Integer> tm = new TreeMap<>();
        tm.put("java", 100);
        tm.put("php", 10);
        tm.put("c", 90);
        tm.put("python", 70);
        tm.put("go", 30);
        tm.put("ruby", 15);
        System.out.println(tm);
        System.out.println(tm.keySet());
        System.out.println(tm.values());
        //创建TreeMap对象：
        TreeMap<String, Integer> treeMap = new TreeMap<>();
        System.out.println("初始化后,TreeMap元素个数为：" + treeMap.size());

        treeMap.put("hello", 1);
        treeMap.put("world", 2);
        treeMap.put("my", 3);
        treeMap.put("name", 4);
        treeMap.put("is", 5);
        treeMap.put("huangqiuping", 6);
        treeMap.put("i", 6);
        treeMap.put("am", 6);
        treeMap.put("a", 6);
        treeMap.put("developer", 6);

        Set<Map.Entry<String, Integer>> entries = treeMap.entrySet();
        entries.forEach(System.out::println);
        for (Map.Entry<String, Integer> entry : entries) {

            Integer value = entry.getValue();
            String key = entry.getKey();
            System.out.println("TreeMap元素的key:" + key + ",value:" + value);
        }
    }


    public static void main4(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("k511", "go");
        map.put("k61", "ruby");
        map.put("k72", "php");
        map.put("k221", "c");
        map.put("k232", "java");
        map.put("k33", "python");
        map.put("k", "javascript");

        Collection<String> values = map.values();
        System.out.println(values);

        ArrayList<String> list = new ArrayList<>(values);

        // 转换成 集合  用Collections工具类进行排序
        Collections.sort(list);
        System.out.println(list);

        //降序  已经转陈list集合  就可以直接使用list集合的 .sort()方法进行排序
        Collections.sort(list, Comparator.reverseOrder());
        list.sort(Comparator.reverseOrder());

        System.out.println(list);


        //将map 的key集合 排序
        Set<String> keySet = map.keySet();
        ArrayList<String> list1 = new ArrayList<>(keySet);
        //默认升序
        Collections.sort(list1);
        System.out.println(list1);

        //降序
        list1.sort(Comparator.reverseOrder());
        System.out.println(list1);
        //遍历
        list1.forEach(k -> System.out.print(k + " "));

        System.out.println();

        //根据字符串长度进行排序 (升序)
        list1.sort(Comparator.comparingInt(String::length));
        System.out.println(list1);

        //(降序)
        list1.sort((x, y) -> y.length() - x.length());
        System.out.println(list1);

        //升序
        list1.sort(Comparator.naturalOrder());
        System.out.println(list1);
        //降序
        list1.sort(Comparator.reverseOrder());
        System.out.println(list1);

        //乱序
        Collections.shuffle(list1);
        System.out.println(list1);
    }

    public static void main2(String[] args) {
        //List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        //Map<String, Object> map = new HashMap<>();
        //var set = new HashSet<String>();
        Map<String, String> map = new HashMap<>();
        map.put("1", "java");
        System.out.println(map.size());
        //清空map
        map.clear();
        //true
        System.out.println(map.isEmpty());
        map.put("k1", "java");
        map.put("k2", "javascript");
        map.put("k2", "myjava");
        map.put("k3", "php");
        map.put("k4", "mysql");
        map.put("k5", "redis");
        //false
        System.out.println(map.containsKey("k6"));
        //map.containsKey()
        //false
        System.out.println(map.containsValue("html"));
        //true
        System.out.println(map.containsValue("redis"));
        //map.containsValue()
        //5
        System.out.println(map.size());

        //myjava
        System.out.println(map.get("k2"));
        map.put("k9", "python");
        map.put("k6", "");
        map.remove("k6");
        //读取map中指定的key的值，如果有就返回，如果没有就使用第二个参数为默认值返回
        Object lang = map.getOrDefault("k6", "没有此键 就返回本值默认");
        System.out.println(lang);

        map.remove("k9");
        map.remove("k9", "python");

        Collection<String> col = map.values();
        System.out.println(col);

        for (Object obj : col) {
            System.out.println(obj);
        }

        Set<String> keys = map.keySet();
        System.out.println(keys);
        for (String k : keys) {
            System.out.printf("%s = %s %n", k, map.get(k));
        }

        System.out.println("----------------------------------");
        // 遍历方法二
        map.forEach((k, v) -> System.out.printf("%s = %s %n", k, v));


    }

    public static void main1(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();

        map.put("刻晴", 180);
        map.put("可莉", 120);
        map.put("您光", 210);
        map.put("的极爱按", 11);
        map.put("大件", 18230);
        map.put("鞋子", 1180);

        System.out.println(map.containsKey("刻晴"));
        System.out.println(map.containsValue(120));

        // m.clear();
        // m.size();
        // m.isEmpty()
        // 移除元素

        map.remove("大件");
        System.out.println(map);
        System.out.println(map.containsKey("大件"));


        System.out.println(map.size());

        //遍历
        //lambda表达式
        map.forEach((k, v) -> System.out.printf("%s=%d  ", k, v));

        //转换为set集合
        Set<String> keys = map.keySet();
        for (String key : keys) {
            System.out.print(key + "=" + map.get(key) + " ");
        }
        //转换collection集合
        Collection<Integer> values = map.values();
        for (Integer value : values) {
            System.out.println(value);
        }
    }
}
